<?php
/*
 * Camelot PHP Tools
 *
 * @version 2.0
 * @author Bendsoft
 * @package PHP Tools
 * @subpackage Camelot
 * @license FreeBSD License (www.bendsoft.com/licensing/)
 */

class WcfSettingsList extends ArrayObject {

    /**
     * Store instance of singelton
     * @since 2.1
     * @var WcfSettingsList 
     */
    private static $instance;
    
    /**
     * Construct or retrieve an instance of singelton
     * @since 2.1
     * @return WcfSettingsList
     */
    public static function instance()
    {
        if (!isset(self::$instance)) {
            $className = __CLASS__;
            self::$instance = new $className;
        }
        return self::$instance;
    }
    
    /**
     * Append values to the WcfSettingsList Array
     * @param WcfSettingsObject $value
     */
    public function appendSettings(WcfSettingsObject $value) {
        parent::append($value);
    }


    /**
     * Append values to the WcfSettingsList Array. Strict standards of PHP 
     * doesn't allow us to set the explicit type on inherited parameters.
     * Use the appendSettings instead.
     * @deprecated since version 2.0
     * @param WcfSettingsObject $value 
     */
    public function append($value) {
        if(gettype($value) == "object" && get_class($value) == "WcfSettingsObject"){
            parent::append($value);
        }
    }
    
    /**
     * Get settings for a specific row in the WcfSettingsList Array
     * @param string $connection_name
     * @return WcfSettingsObject 
     */
    public function getSettings($connection_name) {
        $settings = null;
        foreach ($this as $settingsRow) {
            if ($connection_name == $settingsRow->connection_name) {
                $settings = $settingsRow;
                break;
            }
        }
        return $settings;
    }    
}

class WcfSettingsObject {

    /**
     * The connection name in the Camelot SharePoint Integration Service
     * Just a simple reference to the connection. Ie. SharePointSite1 
     * @var string 
     */
    public $connection_name;
    
    /**
     * Url location of WSDL Service
     * @var string 
     */
    public $wsdl_address;
    
    /**
     * Connection string
     * Set which connectionstring to use from the web.config of the wsdl service
     * @var string 
     */
    public $connection_string;
    
    /**
     * Shared key to access the methods in the WCF service
     * Remember to set the same key in the web.config file of the wcf service
     * @var string 
     */
    public $wsdl_shared_key;
    
    /**
     * Set n minutes to cache a queryresult, if set to 0 it defaults to no cache
     * @var int
     */
    public $wsdl_cache;
    
    // Misc properties
    
    /**
     * WSDL Client Setting, SOAP Version
     * @var mixed enum or int
     */
    public $wsdl_soap_version = SOAP_1_1;
    
    /**
     * WSDL Client Setting, exceptions
     * @var bool
     */
    public $wsdl_exceptions = true;
    
    /**
     * WSDL Client Setting, tracing
     * @var bool
     */
    public $wsdl_trace = true;
    
    /**
     * Compress transfered data or not
     * @var bool
     */
    public $compression = true;

    
    /**
     * Authentication method, default or ntlm
     * @var string 
     */
    public $authentication_method = "default";


    /**
     * Construct the WcfSettings Object
     * 
     * @param string $connection_name The connection name in the Camelot SharePoint Integration Service
     * @param string $wsdl_address Url location of WSDL Service
     * @param string $wsdl_shared_key Shared key to access the methods in the WCF service
     * @param string $connection_string Authentication method, default or ntlm
     * @param int $wsdl_cache Set n minutes to cache a queryresult, if set to 0 it defaults to no cache
     * @param string $authentication_method
     * @param string $wsdl_soap_version WSDL Client Setting, SOAP Version
     * @param bool $wsdl_exceptions WSDL Client Setting, exceptions
     * @param bool $wsdl_trace WSDL Client Setting, tracing
     * @param bool $compression Compress transfered data or not
     */
    public function __construct($connection_name, $wsdl_address, $wsdl_shared_key, $connection_string, $wsdl_cache = 0, $authentication_method = "default", $wsdl_soap_version = null, $wsdl_exceptions = null, $wsdl_trace = null, $compression = null) {
        $this->connection_name = $connection_name;
        $this->wsdl_address = $wsdl_address;
        $this->wsdl_shared_key = (preg_match('#^[a-f0-9]{32}$#i', $wsdl_shared_key)) ? $wsdl_shared_key : md5($wsdl_shared_key);
        $this->connection_string = $connection_string;
        $this->wsdl_cache = $wsdl_cache;
        $this->authentication_method = $authentication_method;
        $this->wsdl_soap_version = (is_null($wsdl_soap_version)) ? SOAP_1_1 : $wsdl_soap_version;
        $this->wsdl_exceptions = (is_null($wsdl_exceptions)) ? true : $wsdl_exceptions;
        $this->wsdl_trace = (is_null($wsdl_trace)) ? true : $wsdl_trace;
        $this->compression = (is_null($compression)) ? true : $compression;
    }
}

?>
